from django.shortcuts import render
from rest_framework.viewsets import GenericViewSet
from rest_framework.mixins import ListModelMixin, RetrieveModelMixin
from . import models
from . import serializer
from .paginations import PageNumberPagination  # 导入自己改过的分页器
from rest_framework.filters import OrderingFilter, SearchFilter  # 导入自带的排序和过滤的模块
from django_filters.rest_framework import DjangoFilterBackend  # 导入第三方过滤 django-filter
from .filters import CourseFilterSet  # 导入自定义的过滤类


class CourseCategoryView(GenericViewSet, ListModelMixin):
    """查询课程分类"""
    queryset = models.CourseCategory.objects.filter(is_delete=False, is_show=True).order_by('orders')
    serializer_class = serializer.CourseCategorySerializer


class CourseView(GenericViewSet, ListModelMixin, RetrieveModelMixin):
    """查询课程"""
    queryset = models.Course.objects.filter(is_delete=False, is_show=True).order_by('orders')
    serializer_class = serializer.CourseModelSerializer
    pagination_class = PageNumberPagination  # 使用分页器

    # 排序和过滤
    filter_backends = [DjangoFilterBackend, OrderingFilter]  # 用django-filter来过滤，自带的OrderingFilter来排序
    ordering_fields = ['id', 'price', 'students']  # 可排序的字段，其他字段无效
    filter_fields = ['course_category']  # 可过滤的字段，其他字段无效

    # # 通过django-filter扩展过滤
    # filter_backends = [DjangoFilterBackend]  # 依然要用DjangoFilterBackend
    # # 上面是配置字段，这里是配置类，类中配置了字段
    # filter_class = CourseFilterSet
    """
    排序：
        使用自带的 OrderingFilter
            URL：http://127.0.0.1:8000/course/free/?ordering=-price
            配置类：filter_backends = [OrderingFilter]
            配置字段：ordering_fields = ['id', 'price']
        
    过滤：
        使用自带的 SearchFilter
            不支持外键字段的过滤
            URL：http://127.0.0.1:8000/course/free/?search=2
            配置类：filter_backends = [SearchFilter]
            配置字段：search_fields = ['id']
            
        使用第三方扩展 django-filter
            安装：pip3 install django-filter
            支持自有字段的过滤，还支持外键字段的过滤
            用法一：
                URL：http://127.0.0.1:8000/course/free/?course_category=1  # 过滤：获取课程分类为1的数据
                配置类：filter_backends = [DjangoFilterBackend]
                配置字段：filter_fields = ['course_category']
            用法二：
                URL：http://127.0.0.1:8000/course/free/?course_category=1
                配置类：filter_backends = [DjangoFilterBackend]
                配置自己写的类：filter_class = CourseFilterSet
                    class CourseFilterSet(FilterSet):
                        class Meta:
                            model = models.Course
                            fields = ['course_category']  # 在类中配置支持过滤的字段
            用法三：实现区间过滤
                URL：http://127.0.0.1:8000/course/free/?course_category=1&min_price=50&max_price=90
                在用法二的基础上，给自己写的类加两行代码：
                class CourseFilterSet(FilterSet):
                    # 课程的价格范围要大于min_price，小于max_price
                    min_price = filters.NumberFilter(field_name='price', lookup_expr='gt')  # gt 大于  gte 大于等于
                    max_price = filters.NumberFilter(field_name='price', lookup_expr='lt')  # lt 小于  lte 小于等于
                
                    class Meta:
                        model = models.Course
                        fields = ['course_category']
    """


class CourseChapterView(GenericViewSet, ListModelMixin):
    """查询章节"""
    queryset = models.CourseChapter.objects.filter(is_delete=False, is_show=True)
    serializer_class = serializer.CourseChapterSerializer
    # 过滤
    filter_backends = [DjangoFilterBackend]
    filter_fields = ['course']  # 按照课程id号过滤


class CourseSearchView(GenericViewSet, ListModelMixin):
    """搜索接口"""
    queryset = models.Course.objects.filter(is_delete=False, is_show=True)
    serializer_class = serializer.CourseModelSerializer
    pagination_class = PageNumberPagination  # 使用分页器

    filter_backends = [SearchFilter]
    search_fields = ['name']  # 可按照name进行搜索

